package com.qrxl.common;

import com.alibaba.fastjson.JSONObject;

import com.qrxl.enums.HjReturnCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * action ajax封装json数据的对象
 *
 * @author xieyuanqing
 * @2015年8月15日
 */
@ApiModel
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = -1793734311569686636L;

    @ApiModelProperty(value = "状态码,非200为失败", required = true)
    private Integer code;

    @ApiModelProperty(value = "状态说明", required = true)
    private String message;

    @ApiModelProperty(value = "数据对象")
    private T data;

    /**
     * 不允许外部new对象
     */
    private JsonResult() {
    }

    /**
     * 取得返回JsonResult对象
     *
     * @param hjReturnCode
     * @return
     */
    public static JsonResult genJsonResult(HjReturnCode hjReturnCode) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(hjReturnCode.getCode());
        jsonResult.setMessage(hjReturnCode.getMsg());
        return jsonResult;
    }

    /**
     * 返回成功的数据
     *
     * @return
     */
    public static <T> JsonResult<T> genJsonResultByOk() {
        HjReturnCode returnCode = HjReturnCode.CODE_OK;
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setCode(returnCode.getCode());
        jsonResult.setMessage(returnCode.getMsg());
        return jsonResult;
    }

    /**
     * 返回成功的数据
     *
     * @param data
     * @return
     */
    public static <T> JsonResult<T> genJsonResultByOk(T data) {
        HjReturnCode returnCode = HjReturnCode.CODE_OK;
        JsonResult<T> jsonResult = new JsonResult<T>();
        jsonResult.setCode(returnCode.getCode());
        jsonResult.setMessage(returnCode.getMsg());
        jsonResult.setData(data);
        return jsonResult;
    }

    /**
     * 取得返回JsonResult对象
     *
     * @param hjReturnCode
     * @param data
     * @return
     */
    public static <T> JsonResult<T> genJsonResult(HjReturnCode hjReturnCode, T data) {
        JsonResult<T> jsonResult = new JsonResult<T>();
        jsonResult.setCode(hjReturnCode.getCode());
        jsonResult.setMessage(hjReturnCode.getMsg());
        jsonResult.setData(data);
        return jsonResult;
    }

    /**
     * 取得返回JsonResult对象
     *
     * @param hjReturnCode
     * @param msgData
     * @return 返回信息的动态数据
     */
    public static JsonResult genJsonResultWithMsgData(HjReturnCode hjReturnCode, Object... msgData) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(hjReturnCode.getCode());
        if (msgData != null && msgData.length > 0 && hjReturnCode.getMsg() != null) {
            jsonResult.setMessage(String.format(hjReturnCode.getMsg(), msgData));
        } else {
            jsonResult.setMessage(hjReturnCode.getMsg());
        }
        return jsonResult;
    }

    /**
     * 判断结果是否成功
     *
     * @return
     */
    public boolean success() {
        if (this.getCode() != null && this.getCode() == HjReturnCode.CODE_OK.getCode().intValue()) {
            return true;
        }
        return false;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 获取 数据
     *
     * @return
     */
    public T getData() {
        return data;
    }

    /**
     * 设置 数据
     *
     * @param data
     */
    public void setData(T data) {
        this.data = data;
    }

    public Object toJSONObject() {
        return JSONObject.toJSON(this);
    }

}
